<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客户编号" prop="customerNo">
        <el-input
          v-model="queryParams.customerNo"
          placeholder="请输入客户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快捷码" prop="shortcutCode">
        <el-input
          v-model="queryParams.shortcutCode"
          placeholder="请输入快捷码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售人员" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入销售人员姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有效" prop="deleteFlag">
        <el-select
          v-model="queryParams.deleteFlag"
          placeholder="请选择是否有效"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in deleteFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['systembasic:customer:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['systembasic:customer:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['systembasic:customer:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['systembasic:customer:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="customerList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" align="center" prop="customerNo" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="快捷码" align="center" prop="shortcutCode" />
      <el-table-column label="客户地址" align="center" prop="address" />
      <el-table-column label="客户联系人" align="center" prop="contactor" />
      <el-table-column label="移动电话" align="center" prop="telephoneNumber" />
      <el-table-column label="固定电话" align="center" prop="phonenumber" />
      <el-table-column label="销售人员" align="center" prop="employeeName" />
      <el-table-column
        label="是否有效"
        align="center"
        prop="deleteFlag"
        :formatter="deleteFlagFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['systembasic:customer:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['systembasic:customer:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 选择销售人员对话框 -->
    <el-dialog
      :title="selectEmployeeTitle"
      :visible.sync="selectEmployeeOpen"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <div style="height:500px">
        <el-card>
          <el-form
            :model="queryEmployeeParams"
            ref="queryEmployeeForm"
            :inline="true"
            label-width="68px"
          >
            <el-form-item label="快捷码" prop="shortcutCode">
              <el-input
                v-model="queryEmployeeParams.shortcutCode"
                placeholder="请输入快捷码"
                clearable
                size="small"
                @keyup.enter.native="handleEmployeeQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="cyan"
                icon="el-icon-search"
                size="mini"
                @click="handleEmployeeQuery"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>

          <el-table height="200px"
            ref="emtb"
            v-loading="loading"
            :data="employeeList"
            @selection-change="handleEmployeeSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="员工编号"
              align="center"
              prop="employeeNo"
            />
            <el-table-column
              label="员工姓名"
              align="center"
              prop="employeeName"
            />
            <el-table-column
              label="快捷码"
              align="center"
              prop="shortcutCode"
            />
            <el-table-column
              label="部门"
              align="center"
              prop="dept.deptName"
              :show-overflow-tooltip="true"
            />
          </el-table>

          <pagination
            v-show="totalEmployee > 0"
            :total="totalEmployee"
            :page.sync="queryEmployeeParams.pageNum"
            :limit.sync="queryEmployeeParams.pageSize"
            @pagination="getEmployeeList"
          />
          <el-form label-width="100px">
            <el-form-item
              style="text-align: center;margin-left:-100px;margin-top:50px;"
            >
              <el-button type="primary" @click="confirmSelect">确 定</el-button>
              <el-button @click="cancelSelectEmployee">取 消</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <!-- <el-col :span="20" :xs="24"> -->

        <!-- </el-col> -->
      </div>
    </el-dialog>
    <!-- 添加或修改客户管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      height="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="销售人员" prop="employeeNo">
          <el-input v-model="form.employeeNo" placeholder="销售人员编号" />
          
        </el-form-item>
          </el-col>
          <el-col :span="6" align="center">
            <el-button
            type="primary"
            
            size="mini"
            @click="handleSelectEmployee"
            >选 择</el-button
          >
          </el-col>
        </el-row>
        
        <el-form-item label="客户编号" prop="customerNo">
          <el-input v-model="form.customerNo" v-bind:disabled="disableEdit" placeholder="请输入客户编号" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="快捷码" prop="shortcutCode">
          <el-input v-model="form.shortcutCode" placeholder="请输入快捷码" />
        </el-form-item>
        <el-form-item label="客户地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入客户地址" />
        </el-form-item>
        <el-form-item label="客户联系人" prop="contactor">
          <el-input v-model="form.contactor" placeholder="请输入客户联系人" />
        </el-form-item>
        <el-form-item label="移动电话" prop="telephoneNumber">
          <el-input
            v-model="form.telephoneNumber"
            placeholder="请输入移动电话"
          />
        </el-form-item>
        <el-form-item label="固定电话" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入固定电话" />
        </el-form-item>
        <el-form-item label="删除标志" prop="deleteFlag">
          <el-radio-group v-model="form.deleteFlag">
            <el-radio
              v-for="dict in deleteFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomer,
  getCustomer,
  delCustomer,
  addCustomer,
  updateCustomer,
  exportCustomer
} from "@/api/systembasic/customer";
import { listEmployee } from "@/api/systembasic/employee";
export default {
  name: "Customer",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      deleteFlagOptions: [],
      //是否显示弹出选择框
      selectEmployeeOpen: false,
      //选中的销售员工
      selectedEmployeeNo: "",
      //员工表格数据
      employeeList: [],
      // 总员工条数
      totalEmployee: 0,
      //选择员工弹出框标题
      selectEmployeeTitle: "",
      disableEdit:true,
      //员工查询参数
      queryEmployeeParams: {
        pageNum: 1,
        pageSize: 10,
        employeeNo: null,
        employeeName: null,
        shortcutCode: null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerNo: null,
        customerName: null,
        shortcutCode: null,
        employeeName: null,
        deleteFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerNo: [
          { required: true, message: "客户编号不能为空", trigger: "blur" }
        ],
        employeeNo: [
          { required: true, message: "销售人员编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.deleteFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否有效字典翻译
    deleteFlagFormat(row, column) {
      return this.selectDictLabel(this.deleteFlagOptions, row.deleteFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rowId: null,
        customerNo: null,
        customerName: null,
        shortcutCode: null,
        address: null,
        contactor: null,
        telephoneNumber: null,
        phonenumber: null,
        employeeNo: null,
        deleteFlag: "N",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.disableEdit=false;
      this.reset();
      this.open = true;
      this.title = "添加客户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disableEdit=true;
      this.reset();
      const customerNo = row.customerNo || this.ids;
      getCustomer(customerNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rowId != null) {
            updateCustomer(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerNos = row.customerNo || this.ids;
      this.$confirm(
        '是否确认删除客户管理编号为"' + customerNos + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delCustomer(customerNos);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有客户管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportCustomer(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    },
    handleSelectEmployee() {
      this.selectEmployeeOpen = true;
      this.selectEmployeeTitle = "选择销售人员";
      this.getEmployeeList();
    },
    // 取消选择销售人员按钮
    cancelSelectEmployee() {
      this.selectEmployeeOpen = false;
    },
    confirmSelect() {
      this.selectEmployeeOpen = false;
      this.form.employeeNo = this.selectedEmployeeNo;
    },
    handleEmployeeSelectionChange(selection) {
      this.selectedEmployeeNo = "";
      if (selection.length > 0) {
        this.selectedEmployeeNo = selection[0].employeeNo;
      }
      if (selection.length > 1) {
        this.$refs.emtb.clearSelection();
        this.$refs.emtb.toggleRowSelection(selection.pop());
      }
    },
    getEmployeeList() {
      this.loading = true;
      listEmployee(this.queryEmployeeParams).then(response => {
        this.employeeList = response.rows;
        this.totalEmployee = response.total;
        this.loading = false;
      });
    },
    handleEmployeeQuery() {
      this.queryEmployeeParams.pageNum = 1;
      this.getEmployeeList();
    }
  }
};
</script>
